﻿@{
    ViewData["Title"] = "WarehouseUpd";
    Layout = "~/Views/Shared/Template.cshtml";
}

<h4>编辑仓库</h4>
<!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
<form class="layui-form" action="/Warehouse/WarehouseUpd" method="post">
    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库编号:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseNo" id="warehouseNo" lay-verify="required" placeholder="可不填写，自动生成" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">部门:</label>
            <div class="layui-input-block">
                <select id="departmentId" name="departmentId" lay-verify="required">
                    <option value="">请选择</option>
                    <option value="0">财务中心</option>
                    <option value="1">信息技术中心</option>
                    <option value="2">采购中心</option>
                    <option value="3">销售中心</option>
                    <option value="4">行政部</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库名称:</label>
            <div class="layui-input-block">
                <input type="text" id="warehouseName" name="warehouseName" required lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">仓库类型:</label>
            <div class="layui-input-block">
                <select name="warehouseTypeId" id="warehouseTypeId" lay-filter="aihao"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">作用:</label>
            <div class="layui-input-block">
                <input type="text" name="function" id="function" placeholder="请输入仓库作用" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">租赁时间:</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="date" name="leaseDate" id="leaseDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">面积:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseArea" id="warehouseArea" placeholder="请填写面积" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">地址:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseAddress" id="warehouseAddress" placeholder="请输入仓库地址" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-row">
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">联系人:</label>
            <div class="layui-input-block">
                <input type="text" name="warehouseContact" id="warehouseContact" placeholder="请填写联系人" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-col-sm6">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-block">
                <input type="text" name="warehousePhone" id="warehousePhone" placeholder="请填写电话" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
        </div>
    </div>
</form>
<script>
    //文档就绪函数
    $(function () {
        warehouseType();//仓库类型下拉框
        fantian();//反填
    });

    //仓库类型下拉框
    function warehouseType() {
        layui.use('form', function () {
            var form = layui.form;
            $.ajax({
                url: "/Warehouse/GetWarehouseTypeShow",
                type: "get",
                data: { token: localStorage.getItem("token") },
                success: function (data) {
                    var data = JSON.parse(data);
                    console.log(data.data)
                    var options = '<option value="">请选择</option>'; // 添加默认选项
                    $.each(data.data, function (index, item) {
                        options += '<option value="' + item.warehouseTypeId + '">' + item.warehouseTypeName + '</option>';
                    });
                    $("#warehouseTypeId").html(options);
                    form.render('select'); // 重新渲染下拉框
                },
                error: function () {
                    console.log("获取数据失败");
                    // 可以考虑显示一个错误消息给用户
                }
            });
        });
    }
    var obj = {
        warehouseId: 0,
        whetherDefault: '',
        whetherDisable: '',
        createName: '',
        createTime: '',
        updateName: '',
        updateTime: '',
        deleteName: '',
        deleteTime: '',
        isDelete: ''
    };

    //反填
    function fantian() {
        return new Promise(function (resolve, reject) {
            $(document).ready(function () {
                // 获取要编辑的用户ID，可以通过URL参数或者其他方式获取，这里假设通过URL参数获取
                var warehouseId = localStorage.getItem("warehouseId");
                console.log(warehouseId);
                // 如果获取到了用户ID，就去后台获取该用户的详细信息并反填到表单
                if (warehouseId) {
                    $.ajax({
                        type: "get",
                        url: "/Warehouse/GetFanWarehouse",
                        data: {
                            warehouseId: warehouseId,
                            token: localStorage.getItem("token")
                        },
                        success: function (res) {
                            var res = JSON.parse(res);
                            if (res.data) {
                                const warehouseData = res.data;
                                // 将获取到的用户数据反填到表单相应字段
                                $('#warehouseNo').val(warehouseData.warehouseNo);
                                $('#warehouseName').val(warehouseData.warehouseName);
                                $('#function').val(warehouseData.function);
                                $('#warehouseArea').val(warehouseData.warehouseArea);
                                $('#warehouseAddress').val(warehouseData.warehouseAddress);
                                $('#warehouseContact').val(warehouseData.warehouseContact);
                                $('#warehousePhone').val(warehouseData.warehousePhone);
                                // 将后台返回的时间格式转换为前端期望的格式
                                $('#departmentId').val(warehouseData.departmentId);
                                layui.form.render('select')
                                $('#warehouseTypeId').val(warehouseData.warehouseTypeId);//下拉框反填数据
                                layui.form.render('select')
                                var date = new Date(warehouseData.leaseDate);
                                var year = date.getFullYear();
                                var month = ("0" + (date.getMonth() + 1)).slice(-2);
                                var day = ("0" + date.getDate()).slice(-2);
                                $('#leaseDate').val(year + "-" + month + "-" + day);//时间反填数据
                                obj.warehouseId = warehouseData.warehouseId;
                                obj.whetherDefault = warehouseData.whetherDefault
                                obj.whetherDisable = warehouseData.whetherDisable
                                obj.createName = warehouseData.createName;
                                obj.createTime = warehouseData.createTime;
                                obj.updateName = warehouseData.updateName;
                                obj.updateTime = warehouseData.updateTime;
                                obj.deleteName = warehouseData.deleteName;
                                obj.deleteTime = warehouseData.deleteTime;
                                obj.isDelete = warehouseData.isDelete;
                                console.log(res.data);
                                console.log(obj.warehouseId);
                                resolve(warehouseData);
                            } else {
                                layui.layer.msg(res.message, { icon: 2 });
                                reject(res.message);
                            }
                        },
                        error: function () {
                            layui.layer.msg('获取用户信息失败，请稍后重试', { icon: 2 });
                            reject('获取用户信息失败');
                        }
                    });
                } else {
                    reject('未获取到有效的仓库ID');
                }
            });
        });
    }

    var obj1 = {
        warehouseId: 0,
        warehouseNo: '',
        warehouseName: '',
        function: '',
        warehouseArea: '',
        warehouseAddress: '',
        warehouseContact: '',
        warehousePhone: '',
        departmentId: '',
        warehouseTypeId: '',
        leaseDate: '',
        whetherDefault: '',
        whetherDisable: '',
        createName: '',
        createTime: '',
        updateName: '',
        updateTime: '',
        deleteName: '',
        deleteTime: '',
        isDelete: ''
    };
    layui.use(['layer', 'form', 'util'], function () {
        var layer = layui.layer;
        // var $ = layui.layer;
        var form = layui.form;
        var util = layui.util;

        // 表单提交事件
        form.on('submit(formDemo)', function (data) {
            var field = data.field; // 获取表单字段值

            obj1.warehouseNo = field.warehouseNo,
                obj1.warehouseName = field.warehouseName,
                obj1.function = field.function,
                obj1.warehouseArea = field.warehouseArea,
                obj1.warehouseAddress = field.warehouseAddress,
                obj1.warehouseContact = field.warehouseContact,
                obj1.warehousePhone = field.warehousePhone,
                obj1.departmentId = field.departmentId,
                obj1.warehouseTypeId = field.warehouseTypeId,
                obj1.leaseDate = field.leaseDate,
                obj1.warehouseId = obj.warehouseId,
                obj1.whetherDefault = obj.whetherDefault,
                obj1.whetherDisable = obj.whetherDisable,
                obj1.createName = obj.createName,
                obj1.createTime = obj.createTime,
                obj1.updateName = obj.updateName,
                obj1.updateTime = obj.updateTime,
                obj1.deleteName = obj.deleteName,
                obj1.deleteTime = obj.deleteTime,
                obj1.isDelete = obj.isDelete
            // 显示填写结果，仅作演示用
            // 此处可执行 Ajax 等操作

            $.ajax({
                url: '/Warehouse/GetUpdWarehouse',
                type: 'post',
                data: {
                    obj1: obj1,
                    token: localStorage.getItem("token")
                },
                success: function (res) {
                    var res = JSON.parse(res);
                    console.log(field);
                    console.log(res);
                    if (res.code == 1) {
                        layer.msg("修改成功", { icon: 1 })
                         //跳转页面
                         setTimeout(function () {
                        location.href = '/Warehouse/WarehouseShow'
                    }, 2000);
                    } else {
                        layer.msg("修改失败:" + res.message, { icon: 2 });
                    }
                }
            });

            return false; // 阻止默认 form 跳转
        });

        // 对弹层中的表单进行初始化渲染
        form.render();
    });


</script>

